<template>
  <div class="shopContainer">
    <div class="header">
      <van-icon @click="toHome" name="wap-home-o" />
      <span class="value">值得买</span>
      <van-icon @click="toSearch" class="search" name="search" />
      <van-icon @click="toCart" name="cart-o" />
    </div>

    <div class="body">

      <div class="navText">
        <img class="textImg" src="http://yanxuan.nosdn.127.net/0b7676e645253f84be662aacfc051922.png" alt="">
        <span class="title">严选好物,放心生活</span>
        <div class="swiperBox">
          <van-swipe style="height: 325px;" class="my-swipe" :loop="false" :autoplay="2000" :width="88">
            <van-swipe-item v-for="(itemlist, index) in swiperList " :key="index">
              <div class="swiperItem" v-for="(item, index2) in itemlist" :key="index2">
                <div class="img-class">
                  <img class="itemImg" :src='item.picUrl'>
                  <span class="text1">{{item.mainTitle}}</span>
                  <span class="text2">{{item.viceTitle}}</span>
                </div>
              </div>
            </van-swipe-item>
          </van-swipe>
        </div>
      </div>

      <!-- 瀑布流 -->
      <div v-masonry class="masonry" transition-duration="0.002s" item-selector=".item" gutter=0>

        <div v-masonry-tile v-for="(item, index) in waterData as any" :key="index" class="item">
          <img :src="item.picUrl" />
          <div class="title">{{item.subTitle}}</div>
          <div class="bottom">

            <div class="bottombox">
              <img class="bottomImg" :src="item.avatar" alt="">
              <span class="bottomSpan">{{item.nickname}}</span>
            </div>

            <div class="bottombox">
              <img class="bottomImg" src="https://yanxuan.nosdn.127.net/5097bc5f2e1eb15f2a32b56895db073a.png" alt="">
              <span class="bottomSpan">{{Math.floor(item.readCount / 1000)}}k</span>
            </div>
          </div>
        </div>

      </div>

    </div>

    <TabbarServe></TabbarServe>
  </div>
</template>

<script lang="ts">
// import TabbarServe from '../../components/Tabbar/TabbarServe.vue';
import { defineComponent, nextTick, onMounted, onBeforeUnmount ,ref} from 'vue';

export default defineComponent({
  name: 'Shop'
  // components: {
  //   TabbarServe
  // }
});
</script>

<script lang="ts" setup>
import { userShopStore } from '@/store/shop';
import router from '@/router';
const shopStore = userShopStore();

//跳转路由
//home
const toHome = () => {
  router.push('/home');
};
//搜索
const toSearch = () => {
  router.push('/search');
};
//购物车

const toCart = () => {
  router.push('/cart');
};

// console.log(list);
//整理轮播图数据的函数
const groupList = (list: any) => {
  const arr = [];
  for (var i = 0; i < list.length; i += 2) {
    arr.push(list.slice(i, i + 2));
  }
  return arr;
};
const swiperList = ref<any>();
const waterData = ref([]);
const page = ref(1);
const size = ref(2);
nextTick(async () => {
  await shopStore.getShopStoreData();
  await shopStore.getWaterData(page.value, size.value);
  waterData.value = shopStore.topics;
  const list = shopStore.shopSwiper;
  swiperList.value = groupList(list);
  // console.log(111, waterData);
});

// console.log(swiperList);

//监听页面滚动的函数
const handleScroll = () => {
  window.addEventListener('scroll', () => {
    let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    let windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
    let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
    // console.log(scrollTop,windowHeight,scrollHeight);

    if (Math.ceil(scrollTop) + Math.ceil(windowHeight) == Math.ceil(scrollHeight)) {
      // console.log(scrollTop,windowHeight,scrollHeight);
      // console.log("到了底部")
      shopStore.getWaterData(page.value++, size.value++);
      waterData.value = shopStore.topics;
    }
  });
};
onMounted(() => {
  handleScroll();
});
onBeforeUnmount(() => {
  window.removeEventListener('scroll', () => {
    console.log('xiezai');
  });
});
</script>

<style scoped lang="less">
* {
  margin: 0;
  padding: 0;
}
.shopContainer {
  height: 100%;
  position: relative;
  background-color: #f8f9f9;
  .header {
    width: 100%;
    height: 55px;
    padding: 0 15px;
    // background-color: pink;
    display: flex;
    align-items: center;
    position: fixed;
    top: 0;
    z-index: 999;
    overflow: hidden;
    background-color: white;
    box-sizing: border-box;
    .value {
      flex: auto;
      text-align: center;
      font-size: 16px;
    }
    .search {
      margin: 0 15px;
    }
  }

  .body {
    position: relative;

    .navText {
      width: 100%;
      height: 285px;
      background-image: linear-gradient(to bottom, #c5a565, #f8f9f9);
      position: relative;
      .textImg {
        width: 71px;
        height: 38px;
        position: absolute;
        top: 90px;
        left: 10px;
      }
      .title {
        color: white;
        position: absolute;
        top: 105px;
        left: 85px;
        font-size: 14px;
      }

      .swiperBox {
        // van-swipe__indicator van-swipe__indicator--active
        //   :deep(.van-swipe__indicator--active) {
        //   background: #58fa0b;
        //   // width: 150px;
        //   // height: 200px;
        // }

        :deep(.van-swipe__indicator) {
          background: #d9d9d9;
          width: 15px;
          height: 2px;
        }
        // van-swipe__indicator van-swipe__indicator--active
        :deep(.van-swipe__indicator--active) {
          background: #dd1a21;
        }
        height: 330px;
        width: 94%;
        background-color: white;
        position: absolute;
        top: 135px;
        left: 3%;
        border-radius: 6px;
        overflow: hidden;
        box-sizing: border-box;

        .swiperItem {
          display: flex;
          flex-direction: column;
          align-items: center;
          box-sizing: border-box;
          .img-class {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin: 10px 0;
            // border: 1px solid;
            box-sizing: border-box;
            .itemImg {
              width: 62px;
              height: 66px;
              border-radius: 8px;
            }
            .text1 {
              width: 100%;
              font-size: 15px;
              color: #333333;
              margin: 5px 0;
              text-align: center;
              line-height: 22px;
            }
            .text2 {
              width: 100%;
              font-size: 14px;
              color: #999999;
              text-align: center;
              line-height: 16px;
            }
          }
        }
      }
    }

    .masonry {
      // position: absolute;
      // top: 600px;
      // width: 100%;
      // height: 500px;
      margin-top: 200px;
      // background-color: pink;
      .item {
        background-color: #fff;
        margin-bottom: 15px;
        img {
          width: 170px;
          // height: 180px;
          margin: 0 8px;
          border-radius: 8px;
        }
        .title {
          font-size: 15px;
          width: 170px;
          // height: 32px;
          padding: 0 10px;
          box-sizing: border-box;
        }
        .bottom {
          display: flex;
          justify-content: space-evenly;
          // padding: 0 10px;
          width: 180px;
          //  background-color: green;
          box-sizing: border-box;
          height: 48px;
          .bottombox {
            display: flex;
            justify-content: center;
            align-items: center;
            .bottomImg {
              width: 26px;
              height: 26px;
              border-radius: 50%;
            }
            .bottomSpan {
              font-size: 12px;
            }
          }
        }
      }
    }
  }
}
</style>